import * as Api from "../../api/invoice";
const app = getApp();
// invoiced 已开票
// re_invoice_review 重开审核中
// re_invoice_rejected 驳回修正
// re_invoice_failed 重开失败
// cancelled 已退票
const aa = [
  {
    id: "1",
    status: "invoiced",
    amount: "50",
    add_time: "2021-04-09  08:00:00",
  },
  {
    id: "2",
    status: "re_invoice_review",
    amount: "50",
    add_time: "2021-04-09  08:00:00",
  },
  {
    id: "3",
    status: "re_invoice_rejected",
    amount: "50",
    add_time: "2021-04-09  08:00:00",
  },
  {
    id: "4",
    status: "cancelled",
    amount: "50",
    add_time: "2021-04-09  08:00:00",
  },
  {
    id: "4",
    status: "re_invoice_failed",
    amount: "50",
    add_time: "2021-04-09  08:00:00",
  },
];
Page({
  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    total_page: "",
    current_page: 1,
    count: 0,
    size: 10,
    loading: false, // 加载中状态
    noMoreData: false, // 是否有更多数据
    toastInvoiceTime: "../../images/toast-invoice-time.png",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.fetchData();
  },

  fetchData() {
    if (this.data.loading || this.data.noMoreData) return;

    this.setData({ loading: true });

    Api.getInvoicedList().then((res) => {
      const { total_page, current_page, count, size } = res.paging;
      this.setData({
        list: res.list_data,
        total_page,
        current_page,
        count,
        size,
      });
    });
  },
  onReachBottom() {
    // 判断是否还有更多数据可以加载
    if (this.data.hasMoreData) {
      this.setData({ current_page: this.data.current_page + 1 }); // 页数加1
      this.fetchData(); // 加载下一页数据
    } else {
      wx.showToast({
        title: "没有更多数据了",
        icon: "none",
      });
    }
  },
  onInvoiceDetail(e) {
    const { id, dataset } = e.currentTarget;
    console.log(e);
    if (dataset.status == "cancelled" || dataset.status == "re_invoice_failed")
      return;
    wx.navigateTo({ url: `/pages/invoiceDetail/invoiceDetail?id=${id}` });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
});
